package com.kayt.cyberBar.statistics.job;

import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Calendar;
import java.util.Date;

/**
 * Description
 *
 * @author Kevin
 * @since 12/30/2017 18:24
 */
@Component
public class StatisticsMonth implements IStatistics {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private EntityDao entityDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-1);
        Date date = calendar.getTime();
        executeCall(date,"cyber_statistics_month");
        executeCall(date,"cyber_statistics_finance_month");
        executeCall(date,"company_statistics_month");
    }

    @Transactional(rollbackFor = Exception.class)
    public void executeCall(Date date, String sql) {
        Query query = entityDao.getEntityManager().createNativeQuery("CALL "+sql+"(?)");
        query.setParameter(1, date);
        query.executeUpdate();
    }
    
    public static void main(String[] args) {
    	Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-1);
        Date date = calendar.getTime();
        System.out.println(date);
	}
}
